package com.dly.hope.servce.impl;

import com.dly.hope.modle.Session;
import com.dly.hope.modle.WebSocketMemberMessage;
import com.dly.hope.modle.WebSocketMessage;
import com.dly.hope.repository.GroupUserRepository;
import com.dly.hope.repository.SessionRepository;
import com.dly.hope.repository.WebSocketMemberMessageRepository;
import com.dly.hope.repository.WebSocketMessageRepository;
import com.dly.hope.servce.SaveService;
import com.dly.hope.type.DataStatusEnum;
import com.dly.hope.type.SessionType;
import com.dly.hope.webSocket.dto.MessageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

/**
 * @author linyu.dai
 * @project myserve
 * @description
 * @creat 2022/11/8 13:24:45
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class SaveServiceImpl implements SaveService {

    @Resource
    private WebSocketMessageRepository webSocketMessageRepository;

    @Resource
    private SessionRepository sessionRepository;

    @Resource
    private GroupUserRepository groupUserRepository;

    @Resource
    private WebSocketMemberMessageRepository webSocketMemberMessageRepository;

    @Override
    public WebSocketMessage saveWebSocketMessage(MessageDto messageDto) {
        WebSocketMessage webSocketMessage = new WebSocketMessage();
        if(messageDto.getSessionType().equals(SessionType.GROUP)){
            webSocketMessage.setIsRead(Boolean.TRUE);
        }else{
            //接受方没有会话时创建
            if(!sessionRepository.findTopByUserIdAndSessionIdAndDataStatus(messageDto.getAcceptUserSocketDto().getUserId(), messageDto.getSendUserSocketDto().getUserId(),1).isPresent()){
                //创建会话
                Session session = new Session();
                session.setUserId(messageDto.getAcceptUserSocketDto().getUserId());
                session.setSessionType(messageDto.getSessionType());
                session.setSessionId(messageDto.getSendUserSocketDto().getUserId());
                sessionRepository.save(session);
            }
        }
        webSocketMessage.setAcceptId(messageDto.getAcceptUserSocketDto().getUserId());
        webSocketMessage.setSendId(messageDto.getSendUserSocketDto().getUserId());
        webSocketMessage.setMessage(messageDto.getMessage());
        webSocketMessage.setMessageType(messageDto.getType());
        webSocketMessage.setSessionType(messageDto.getSessionType());

        return webSocketMessageRepository.save(webSocketMessage);
    }

    @Override
    public void saveWebSocketMemberMessage(MessageDto messageDto, WebSocketMessage webSocketMessage) {
        ArrayList<WebSocketMemberMessage> webSocketMemberMessages = new ArrayList<>();
        ArrayList<Session> sessions = new ArrayList<>();
        groupUserRepository.findAllByGroupIdAndDataStatusAndIsAgree(messageDto.getAcceptUserSocketDto().getUserId(),1,1)
                .stream()
                //过滤掉自己
                .filter(groupUser -> !Objects.equals(groupUser.getMember(), messageDto.getSendUserSocketDto().getUserId()))
                .forEach(groupUser -> {
                    WebSocketMemberMessage webSocketMemberMessage = new WebSocketMemberMessage();
                    webSocketMemberMessage.setAcceptId(groupUser.getMember());
                    webSocketMemberMessage.setGroupId(messageDto.getAcceptUserSocketDto().getUserId());
                    webSocketMemberMessage.setWebSocketMessageId(webSocketMessage.getId());
                    webSocketMemberMessages.add(webSocketMemberMessage);

                    //如果接受方没有创建会话 就创建
                    if(!sessionRepository.findTopByUserIdAndSessionIdAndDataStatus(groupUser.getMember(), messageDto.getAcceptUserSocketDto().getUserId(),1).isPresent()){
                        Session session = new Session();
                        session.setUserId(groupUser.getMember());
                        session.setSessionId(messageDto.getAcceptUserSocketDto().getUserId());
                        session.setSessionType(SessionType.GROUP);
                        sessions.add(session);
                    }
                });
        webSocketMemberMessageRepository.saveAllAndFlush(webSocketMemberMessages);
        sessionRepository.saveAllAndFlush(sessions);
    }

    @Override
    public void creatSession(MessageDto messageDto) {
        Session session = sessionRepository
                .findTopByUserIdAndSessionIdAndDataStatus(messageDto.getAcceptUserSocketDto().getUserId(), messageDto.getSendUserSocketDto().getUserId(), DataStatusEnum.EFFICIENT.getCode())
                .orElseGet(Session::new);
        session.setUserId(messageDto.getAcceptUserSocketDto().getUserId());
        session.setSessionId(messageDto.getSendUserSocketDto().getUserId());
        session.setSessionType(SessionType.ALONG);

        sessionRepository.save(session);
    }

}
